using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace libCSV
{
    public class CSVHelper
    {

        public static DataTable DoubleTabToDataTable(double[,] AppData)
        {
            //DataSet ds=new DataSet();
DataRow tuple=null; 
DataTable table = new DataTable();
DataColumn col1 = new DataColumn();//,Type.GetType("integer"));// ("Col1", Type.GetType("toto"));
DataColumn col2 = new DataColumn();//, Type.GetType("toto"));

for (int i = 0; i <= AppData.GetUpperBound(1); i++)
{
    table.Columns.Add("Col "+(i+1).ToString());
}

for (int j = 0; j <= AppData.GetUpperBound(0); j++)
{
    tuple = table.NewRow();
    for (int i = 0; i <= AppData.GetUpperBound(1); i++)
    {
        tuple[i] = AppData[j, i].ToString();
    }
    table.Rows.Add(tuple);
}           
            
            /*

for (int i = 0; i <= AppData.GetUpperBound(0); i++)
{
    for (int j = 0; j < AppData.GetUpperBound(1); j++)
    {
        tuple = table.NewRow();
        tuple[j] = AppData[i,j].ToString();

    }
    table.Rows.Add(tuple);
}
            
            */

            return table;
            //return ds;
        
        }


        public static double GetMinValue(double[,] AppData,int champ)
        {
            double ret = AppData[0, champ];

            for (int i = 1; i < AppData.GetUpperBound(0); i++)
            {
                if (AppData[i, champ] < ret)
                    ret = AppData[i, champ];
            }
            return ret;
        }

        public static double GetMaxValue(double[,] AppData, int champ)
        {
            double ret = AppData[0, champ];

            for (int i = 1; i < AppData.GetUpperBound(0); i++)
            {
                if (AppData[i, champ] > ret)
                    ret = AppData[i, champ];
            }
            return ret;
        }


        public static double[] GetSubLigne(int Debut, int nombre, double[] tableau)
        {
            double[] ret = new double[nombre];
            for (int i = 0; i < nombre; i++)
            {
                
                ret[i] = tableau[Debut + i];
            }

            return ret;
        }

        public static double[] GetLigne(int ligne, double[,] tableau)
        {
            double[] ret = new double[tableau.GetLength(1)];
            for (int i = 0; i < tableau.GetLength(1); i++)
            {
                ret[i] = tableau[ligne, i];
            }

            return ret;
        }


        public static double[,] CsvFileToDoubleTab(string FilePath)
        {
        double[,] ret = null;
        string[] sLine;
        string[] sDouble;
        
        
        StreamReader sr = new StreamReader(FilePath, Encoding.Default);


        string data = sr.ReadToEnd();
        sr.Close();

            sLine = data.Split('\n');
            for (int i = 0; i < sLine.Length; i++)
            {
                if (sLine[i] != "")
                {
                    sDouble = sLine[i].Split(';');
                    if (ret == null)
                    {
                        ret = new double[sLine.Length, sDouble.Length];
                    }
                    for (int j = 0; j < sDouble.Length; j++)
                    {
                        try
                        {
                            sDouble[j] = sDouble[j].Replace(".", ",");
                            ret[i, j] = double.Parse(sDouble[j]);
                        }
                        catch {
                            ret[i, j] = 0;
                        }
                    }
                }
            }
            

        
        sr.Close();

            return ret;
        }


        public static void DoubleTabToCsvFile(string FilePath, double[,] Tableau)
        {
            StreamWriter sw = new StreamWriter(FilePath,false ,Encoding.Default);
            sw.Write(DoubleTabToString(Tableau));
            sw.Close();
            return;
        }

        public static string DoubleTabToString(double[,] Tableau)
        {
            StringBuilder SB = new StringBuilder();
            for (int i = 0; i <= Tableau.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= Tableau.GetUpperBound(1); j++)
                {
                    if (j == 0)
                    {
                        SB.Append(Tableau[i, j].ToString());
                    }
                    else
                    {
                        SB.Append(";" + Tableau[i, j].ToString());
                    }
                }
                SB.Append("\r\n");
            }

            return SB.ToString();
        }
    }
}
